package org.aokey.springdemo3.July;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;

public class UdpEchoClient1 {
    private DatagramSocket socket=null;
    private String ip;
    private int port;
    public UdpEchoClient1(String ip, int port) throws SocketException {
        this.ip=ip;
        this.port=port;
        socket=new DatagramSocket();
    }
    public void Start() throws IOException {
        Scanner scanner=new Scanner(System.in);
        while(true){
            System.out.println("请输入您要发送的内容：");
            String request=scanner.nextLine();
            DatagramPacket packet=new DatagramPacket(request.getBytes(),request.getBytes().length, InetAddress.getByName(ip),port);
            socket.send(packet);
            DatagramPacket responsePacket=new DatagramPacket(new byte[1024],1024);
            socket.receive(responsePacket);
            String response=new String(responsePacket.getData(),0,responsePacket.getLength());
            System.out.println(response);

        }
    }
    public static void main(String[] args) throws IOException {
        UdpEchoClient1 client1=new UdpEchoClient1("127.0.0.1",8080);
        client1.Start();
    }
}
